home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 23 / Amiga Format AFCD23 (Feb 1998, Issue 107).iso / -seriously_amiga- / shareware / programming / c / asyncio / src / writeasync.c < prev    next >
C/C++ Source or Header  |  1997-12-01  |  1KB  |  50 lines

  1. #include "async.h"
  2.  
  3.  
  4. _LIBCALL LONG
  5. WriteAsync( _REG( a0 ) AsyncFile *file, _REG( a1 ) APTR buffer, _REG( d0 ) LONG numBytes )
  6. {
  7. #ifdef ASIO_NOEXTERNALS
  8.     struct ExecBase    *SysBase = file->af_SysBase;
  9. #endif
  10.     LONG totalBytes = 0;
  11.  
  12.     /* this takes care of NIL: */
  13.     if( !file->af_Handler )
  14.     {
  15.         file->af_Offset        = file->af_Buffers[ 0 ];
  16.         file->af_BytesLeft    = file->af_BufferSize;
  17.         return( numBytes );
  18.     }
  19.  
  20.     while( numBytes > file->af_BytesLeft )
  21.     {
  22.         if( file->af_BytesLeft )
  23.         {
  24.             CopyMem( buffer, file->af_Offset, file->af_BytesLeft );
  25.  
  26.             numBytes    -= file->af_BytesLeft;
  27.             buffer        =  ( APTR ) ( ( ULONG ) buffer + file->af_BytesLeft );
  28.             totalBytes    += file->af_BytesLeft;
  29.         }
  30.  
  31.         if( AS_WaitPacket( file ) < 0 )
  32.         {
  33.             return( -1 );
  34.         }
  35.  
  36.         /* send the current buffer out to disk */
  37.         AS_SendPacket( file, file->af_Buffers[ file->af_CurrentBuf ] );
  38.  
  39.         file->af_CurrentBuf    = 1 - file->af_CurrentBuf;
  40.         file->af_Offset        = file->af_Buffers[ file->af_CurrentBuf ];
  41.         file->af_BytesLeft    = file->af_BufferSize;
  42.     }
  43.  
  44.     CopyMem( buffer, file->af_Offset, numBytes );
  45.     file->af_BytesLeft    -= numBytes;
  46.     file->af_Offset        += numBytes;
  47.  
  48.     return ( totalBytes + numBytes );
  49. }
  50.